package com.syqc.monitor.popup

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.utils.loadStringDImgError
import com.syqc.entity.Legend
import com.syqc.monitor.R
import com.syqc.monitor.databinding.MarkerLegend3Binding


/**
 *@Author Jasper
 *@Time   2020/8/7
 *@Desc   图例弹窗
 */
class LegendPopup3(context: Context) : PopupWindow() {
    private val binding = MarkerLegend3Binding.inflate(LayoutInflater.from(context))
    private val location = IntArray(2)
    private var measuredWidth = 0
    private var measuredHeight = 0
    private val mAdapter: LegendAdapter by lazy { LegendAdapter() }

    init {
        contentView = binding.root
        initView(context)
        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(0)) //设置背景
//        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.WRAP_CONTENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.WRAP_CONTENT //设置SelectPicPopupWindow弹出窗体的高
        isClippingEnabled = false
    }


    private fun initView(context: Context) {
        //设置SelectPicPopupWindow的View
        val recycler = contentView.findViewById<RecyclerView>(R.id.recyclerView)
        recycler.layoutManager = LinearLayoutManager(context)
        recycler.adapter = mAdapter
        recycler.setOnClickListener {
            dismiss()
        }
    }

    fun showAtLocation(parent: View, list: MutableList<Legend>) {
        mAdapter.setList(list)

        contentView.measure(
            View.MeasureSpec.UNSPECIFIED,
            View.MeasureSpec.UNSPECIFIED
        )
        measuredWidth = contentView.measuredWidth
        measuredHeight = contentView.measuredHeight
        parent.getLocationOnScreen(location)
        location[0] = location[0] - measuredWidth - parent.width / 3
        location[1] = location[1] - measuredHeight

        showAtLocation(parent, Gravity.TOP, location[0], location[1])
    }


    internal inner class LegendAdapter :
        BaseQuickAdapter<Legend, BaseViewHolder>(R.layout.item_legend2) {
        override fun convert(holder: BaseViewHolder, item: Legend) {
            item.run {
                holder.setText(R.id.tv_item_legend, name)
                    .setText(R.id.tv_item_num, (holder.layoutPosition + 1).toString())

                holder.getView<ImageView>(R.id.iv_item_legend).loadStringDImgError(context, iconUrl)

                val value = holder.layoutPosition % 2
                if (value == 0) {
                    holder.itemView.setBackgroundResource(R.color.white)
                } else {
                    holder.itemView.setBackgroundResource(R.color.color_F0F)
                }
            }
        }
    }
}